/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.ui.preferences.formatter.tab;

import java.net.URL;

import org.eclipse.dltk.ui.CodeFormatterConstants;
import org.eclipse.dltk.ui.formatter.IFormatterControlManager;
import org.eclipse.dltk.ui.formatter.IFormatterModifyDialog;
import org.eclipse.dltk.ui.util.SWTFactory;
import org.eclipse.php.core.formatter.PHPCodeFormatterConstants;
import org.eclipse.php.internal.formatter.PHPFormatterConstants;
import org.eclipse.php.internal.ui.preferences.formatter.PHPFormatterControlManager;
import org.eclipse.php.internal.ui.preferences.formatter.PHPFormatterMessages;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;

/**
 * @author Sean W. Quinn (swquinn@gmail.com)
 */
public class PHPCodeFormatterIndentationTabPage extends PHPCodeFormatterTabPage {

	/**
	 * The tab policy, whether a user has set his formatter to utilize only
	 * tabs, spaces, or a mixture of both.
	 */
	private Combo tabPolicy;

	/**
	 * The indention size.
	 */
	private Text indentSize;
	private Text tabSize;

	private TabPolicyListener tabPolicyListener;

	/**
   *
   */
	private final String[] tabPolicyItems = new String[] { CodeFormatterConstants.SPACE, CodeFormatterConstants.TAB,
			CodeFormatterConstants.MIXED };

	/**
   *
   */
	private final String[] tabPolicyLabels = new String[] {
			PHPFormatterMessages.IndentationTabPage_general_group_option_tab_policy_SPACE,
			PHPFormatterMessages.IndentationTabPage_general_group_option_tab_policy_TAB,
			PHPFormatterMessages.IndentationTabPage_general_group_option_tab_policy_MIXED };

	private class TabPolicyListener extends SelectionAdapter implements IFormatterControlManager.IInitializeListener {

		private final PHPFormatterControlManager manager;

		public TabPolicyListener(PHPFormatterControlManager manager) {
			this.manager = manager;
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			int index = tabPolicy.getSelectionIndex();
			if (index >= 0) {
				final boolean tabMode = CodeFormatterConstants.TAB.equals(tabPolicyItems[index]);
				manager.enableControl(indentSize, !tabMode);
			}
		}

		public void initialize() {
			final boolean tabMode = CodeFormatterConstants.TAB.equals(manager
					.getString(PHPFormatterConstants.FORMATTER_TAB_CHAR));
			manager.enableControl(indentSize, !tabMode);
		}

	}

	/**
	 * @param dialog
	 */
	public PHPCodeFormatterIndentationTabPage(final IFormatterModifyDialog dialog) {
		super(dialog);
	}

	/**
	 * @param manager
	 * @param parent
	 */
	@Override
	protected void doCreateOptions(final PHPFormatterControlManager manager, final Composite parent) {
		// **
		// Tab policy group.
		createTabPolicyGroup(manager, parent);
		createIndentGroup(manager, parent);
	}

	/**
	 *
	 * @param manager
	 * @param parent
	 */
	private void createTabPolicyGroup(final PHPFormatterControlManager manager, final Composite parent) {
		Group tabPolicyGroup = SWTFactory.createGroup(parent,
				PHPFormatterMessages.IndentationTabPage_general_group_title, COLUMN_SPAN, 1, GridData.FILL_HORIZONTAL);
		tabPolicy = manager.createCombo(tabPolicyGroup, PHPFormatterConstants.FORMATTER_TAB_CHAR,
				PHPFormatterMessages.IndentationTabPage_general_group_option_tab_policy, tabPolicyItems,
				tabPolicyLabels);
		tabPolicyListener = new TabPolicyListener(manager);
		tabPolicy.addSelectionListener(tabPolicyListener);
		manager.addInitializeListener(tabPolicyListener);

		manager.createCheckbox(tabPolicyGroup, PHPFormatterConstants.FORMATTER_USE_TABS_ONLY_FOR_LEADING_INDENTATIONS,
				PHPFormatterMessages.IndentationTabPage_use_tabs_only_for_leading_indentations, COLUMN_SPAN);

		indentSize = manager.createNumber(tabPolicyGroup, PHPFormatterConstants.FORMATTER_INDENTATION_SIZE,
				PHPFormatterMessages.IndentationTabPage_general_group_option_indent_size, COLUMN_SPAN);
		tabSize = manager.createNumber(tabPolicyGroup, PHPFormatterConstants.FORMATTER_TAB_SIZE,
				PHPFormatterMessages.IndentationTabPage_general_group_option_tab_size, COLUMN_SPAN);
		tabSize.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				int index = tabPolicy.getSelectionIndex();
				if (index >= 0) {
					final boolean tabMode = CodeFormatterConstants.TAB.equals(tabPolicyItems[index]);
					if (tabMode) {
						indentSize.setText(tabSize.getText());
					}
				}
			}
		});
	}

	/**
	 *
	 * @param manager
	 * @param parent
	 */
	private void createIndentGroup(final PHPFormatterControlManager manager, final Composite parent) {
		Group indentGroup = SWTFactory.createGroup(parent,
				PHPFormatterMessages.IndentationTabPage_indent_group_title, COLUMN_SPAN, 1, GridData.FILL_HORIZONTAL);

		// ** Indent declarations within class body
		manager.createCheckbox(indentGroup,
				PHPCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_TYPE_HEADER,
				PHPFormatterMessages.IndentationTabPage_class_group_option_indent_declarations_within_class_body, COLUMN_SPAN);

		// ** Add indent checkbox for enum statement, not available currently in PHP.
		//manager.createCheckbox(indentGroup,
		//		DefaultPHPCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ENUM_DECLARATION_HEADER,
		//		PHPFormatterMessages.IndentationTabPage_class_group_option_indent_declarations_within_enum_decl, GRID_WIDTH);

		// ** Add indent checkbox for enum constants, not available currently in PHP.
		//manager.createCheckbox(indentGroup,
		//		DefaultPHPCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ENUM_CONSTANT_HEADER,
		//		PHPFormatterMessages.IndentationTabPage_class_group_option_indent_declarations_within_enum_const, GRID_WIDTH);

		// ** Add indent checkbox for annotation body, not available currently in PHP.
		//manager.createCheckbox(indentGroup,
		//		DefaultPHPCodeFormatterConstants.FORMATTER_INDENT_BODY_DECLARATIONS_COMPARE_TO_ANNOTATION_DECLARATION_HEADER,
		//		PHPFormatterMessages.IndentationTabPage_class_group_option_indent_declarations_within_annot_decl, GRID_WIDTH);

		// ** Add indent checkbox for function body
		manager.createCheckbox(indentGroup,
				PHPCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BODY,
				PHPFormatterMessages.IndentationTabPage_block_group_option_indent_statements_compare_to_body, COLUMN_SPAN);

		// ** Add indent checkbox for block body
		manager.createCheckbox(indentGroup,
				PHPCodeFormatterConstants.FORMATTER_INDENT_STATEMENTS_COMPARE_TO_BLOCK,
				PHPFormatterMessages.IndentationTabPage_block_group_option_indent_statements_compare_to_block, COLUMN_SPAN);

		// ** Add indent checkbox for 'switch' body
		manager.createCheckbox(indentGroup,
				PHPCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_SWITCH,
				PHPFormatterMessages.IndentationTabPage_switch_group_option_indent_statements_within_switch_body, COLUMN_SPAN);

		// ** Add indent checkbox for switch 'case' body
		manager.createCheckbox(indentGroup,
				PHPCodeFormatterConstants.FORMATTER_INDENT_SWITCHSTATEMENTS_COMPARE_TO_CASES,
				PHPFormatterMessages.IndentationTabPage_switch_group_option_indent_statements_within_case_body, COLUMN_SPAN);

		// ** Add indent checkbox for 'break' statements
		manager.createCheckbox(indentGroup,
				PHPCodeFormatterConstants.FORMATTER_INDENT_BREAKS_COMPARE_TO_CASES,
				PHPFormatterMessages.IndentationTabPage_switch_group_option_indent_break_statements, COLUMN_SPAN);

		// ** Add indent checkbox for empty lines
		manager.createCheckbox(indentGroup,
				PHPCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES,
				PHPFormatterMessages.IndentationTabPage_indent_empty_lines, COLUMN_SPAN);
	}

	@Override
	protected URL getPreviewContent() {
		return getClass().getResource("indentationPreview.php"); //$NON-NLS-1$
	}
}
